
import { Node, UITransform, UIRenderer} from "cc";
import { BaseComponent } from "../../../../extensions/cocos-scaffold-3x/assets/ui/base/BaseComponent";
import { Comp } from "../m/CompDef";
import { GpEcsWorld } from "../m/GpEcsWorld";
import { GpEventDef } from "../m/GpEventDef";
import GpWorld from "./GpWorld";
import { gg } from "../../../Game";

import { _decorator } from 'cc';
const { ccclass, property } = _decorator;

export class GpEntityProp {
    constructor(
        public world: GpWorld,
        public ecsWorld: GpEcsWorld,
        public ecsEntityId: number
    ) { }
}
@ccclass("GpEntity")
export class GpEntity extends BaseComponent<GpEntityProp> {
    @property(Node)
    renderRoot: Node = null!;

    render: UIRenderer

    layerNode: Node = null

    transform: Comp.Transform

    onInit(): void {
        this.render = this.renderRoot.getComponentInChildren(UIRenderer)
        this.node.setPosition(-10000, -10000)
        this.layerNode = this.node.parent
        this.node.on(Node.EventType.TOUCH_END, this.onTouch, this)
        this.transform = this.ecsEntity.component(Comp.Transform)
        this.node.getComponent(UITransform).setContentSize(this.transform.radius*2,this.transform.radius*2)
        
    }

    get ecsEntity() {
        return this.prop.ecsWorld.getEntity(this.prop.ecsEntityId)
    }

    onTouch() {
        const move=this.ecsEntity.component(Comp.Move)
        // console.log("touch Entity", this.prop.ecsEntityId,move);
        gg.eventTarget.emit(GpEventDef.onTouchEntity, this)
    }

    unuse(): void {
        this.node.setPosition(-10000, -10000)

        // this.node.off(Node.EventType.TOUCH_START, this.onTouch, this)
    }


}
